/**
 * Project Name:OrgServiceImpl
 * File Name:OrgServiceImpl.java
 * Package Name:com.bwda.dsrs.systemservice.service
 * Date:2019年4月28日
 * Copyright (c) 2019, 江苏保旺达软件有限公司 All Rights Reserved.
 */
package com.bwda.dsrs.systemservice.service.impl;

import com.bwda.dsrs.base.util.Convert;
import com.bwda.dsrs.systemservice.domain.Constants;
import com.bwda.dsrs.systemservice.domain.condition.org.OrgCondition;
import com.bwda.dsrs.systemservice.domain.form.org.OrgCreateForm;
import com.bwda.dsrs.systemservice.domain.form.org.OrgTreeForm;
import com.bwda.dsrs.systemservice.domain.form.org.OrgUserAdminForm;
import com.bwda.dsrs.systemservice.domain.form.org.OrgUserTreeForm;
import com.bwda.dsrs.systemservice.domain.po.org.OrgPo;
import com.bwda.dsrs.systemservice.mapper.OrgMapper;
import com.bwda.dsrs.systemservice.service.OrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 组织菜单管理服务类
 *
 * @author youzhen
 * @date 2019年4月28日
 */
@Service
public class OrgServiceImpl extends AbstractBaseServiceImpl<OrgPo, OrgCondition, OrgMapper> implements OrgService {

    @Autowired
    private OrgMapper orgMapper;

    @Override
    public List<OrgUserAdminForm> queryAdmin() {
        return orgMapper.queryAdmin();
    }

    @Override
    public Integer queryId() {
        return orgMapper.queryId();
    }

    @Override
    public List<OrgTreeForm> queryTreeList(OrgTreeForm orgTreeForm) {
        return orgMapper.queryTreeList(orgTreeForm);
    }

    @Override
    public List<OrgTreeForm> queryTree(List<OrgTreeForm> poList) {
        return this.listGetStree(poList);
    }

    @Override
    public List<OrgTreeForm> queryOrgList(OrgTreeForm form) {
        return orgMapper.queryOrgList(form);
    }

    /**
     * 数据转换Tree
     */
    private List<OrgTreeForm> listGetStree(List<OrgTreeForm> list) {
        List<OrgTreeForm> treeList = new ArrayList<OrgTreeForm>();
        for (OrgTreeForm tree : list) {
            //找到根
            if (null!=tree.getParentId()&&tree.getParentId() == Constants.PARENTID) {
                treeList.add(tree);
            }
            //找到子
            for (OrgTreeForm treeNode : list) {
                if (null!=treeNode.getParentId()&&treeNode.getParentId() == tree.getId()) {
                    if (tree.getChildren() == null) {
                        tree.setChildren(new ArrayList<OrgTreeForm>());
                    }
                    treeNode.setOrgName(treeNode.getOrgName().substring(treeNode.getOrgName().lastIndexOf("\\") + 1));
                    tree.getChildren().add(treeNode);
                }
            }
        }
        return treeList;
    }

    @Override
    public List<OrgTreeForm> queryTreeChildren(int id) {
        return orgMapper.queryTreeChildren(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, String> deleteUserByIds(String ids) {
        Map<String, String> map = new HashMap<>(16);
        String failureMsg = "";
        String successMsg = "";
        int successNum = 0;
        int failureNum = 0;
        Integer[] orgIds = Convert.toIntArray(ids);
        for (Integer orgId : orgIds) {
            int orgCount = orgMapper.queryOrgUser(orgId);
            List<OrgPo> org = orgMapper.queryOrgChildren(orgId);
            if (orgCount < 1 && org.size() < 1) {
                orgMapper.delete(orgId);
                successNum++;
            } else {
                failureNum++;
            }
        }
        if (failureNum > 0) {
            failureMsg = failureNum + "条数据删除失败！该组织下已有用户";
//            map.put("msg", "删除失败！该组织下已有用户或组织");
            map.put("msg", failureMsg);
            map.put("state", "false");
            return map;
        } else {
            successMsg = "操作成功！共 " + successNum + " 条数据";
//            map.put("msg", "操作成功");
            map.put("msg", successMsg);
            map.put("state", "true");
            return map;
        }
    }

    @Override
    public List<OrgUserTreeForm> queryUserOrgList() {
        return orgMapper.queryUserOrgList();
    }

    @Override
    public List<OrgUserAdminForm> queryOrgAdmin(Integer id) {
        return orgMapper.queryOrgAdmin(id);
    }

    @Override
    public List<OrgUserTreeForm> queryUserOrgTree(List<OrgUserTreeForm> poList) {
        return this.listGetUserStree(poList);
    }

    /**
     * 数据转换Tree
     */
    private List<OrgUserTreeForm> listGetUserStree(List<OrgUserTreeForm> list) {
        List<OrgUserTreeForm> treeList = new ArrayList<OrgUserTreeForm>();
        for (OrgUserTreeForm tree : list) {
            //找到根
            if (tree.getParentId() == Constants.PARENTID) {
                treeList.add(tree);
            }
            //找到子
            for (OrgUserTreeForm treeNode : list) {
                if (treeNode.getParentId() == tree.getId()) {
                    if (tree.getChildren() == null) {
                        tree.setChildren(new ArrayList<OrgUserTreeForm>());
                    }
                    tree.getChildren().add(treeNode);
                }
            }
        }
        return treeList;
    }

    @Override
    public OrgCreateForm queryOrg(String userCode) {
        return orgMapper.queryOrg(userCode);
    }

    @Override
    public OrgCreateForm queryByIdOrg(Integer id) {
        return orgMapper.queryByIdOrg(id);
    }

    @Override
    public List<OrgCreateForm> queryOrgById(String ids) {
        Integer[] orgIds = Convert.toIntArray(ids);
        return orgMapper.queryOrgById(orgIds);
    }

    @Override
    public List<OrgUserTreeForm> queryOrgSingleTree(Integer id) {
        return orgMapper.queryOrgSingleTree(id);
    }

    @Override
    public List<OrgCreateForm> queryParentOrgByUserCode(Integer orgId) {
        //原来的mysql递归看不懂，还是自己写代码吧

        List<OrgCreateForm> orgCreateForms = new ArrayList<>();
        recyclyAddParentOrg(orgCreateForms, orgId);
        return orgCreateForms;
    }


    private void recyclyAddParentOrg(List<OrgCreateForm> orgCreateFormList, Integer orgId) {
        OrgCreateForm orgCreateForm = orgMapper.queryByIdOrg(orgId);
        if (orgCreateForm != null && orgCreateForm.getId() != null) {

            orgCreateFormList.add(orgCreateForm);
        }
        //如果查出来的parentOrgId  是0，表示到头了，否则表示还有上级
        if (orgCreateForm != null && orgCreateForm.getParentId() > 0) {
            recyclyAddParentOrg(orgCreateFormList, orgCreateForm.getParentId());
        }


    }


    @Override
    public List<OrgPo> queryName(OrgCondition orgCondition) {
        return orgMapper.queryName(orgCondition);
    }

    @Override
    public void updateOrgAdmin(String userCode, Integer orgId) {
        orgMapper.updateOrgAdmin(userCode, orgId);
    }

    @Override
    public List<OrgPo> queryOrgByUserCode(String userCode) {
        return orgMapper.queryOrgByUserCode(userCode);
    }
}